﻿using System;
using EPiSugar.Content;
using System.Web.UI;

namespace EPiSugar.Markup
{
    public static class TagGeneratorExtensions
    {
        public static TagGenerator Tags(this Control control)
        {
            return new TagGenerator(control.Page);
        }

        public static Tag A(this TagGenerator tags, string href, string text, string cssClass, string target)
        {
            return tags.A(href, text).Attr("target", target).Attr("class", cssClass);
        }
        public static Tag A(this TagGenerator tags, string href, string text)
        {
            return tags.Tag("a", text).Attr("href", href).RenderWhen(() => href != null);
        }

        public static Tag Label(this TagGenerator tags, string text, string cssClass, string forId)
        {
            return tags.Label(text).Attr("for", forId).Attr("class", cssClass);
        }
        public static Tag Label(this TagGenerator tags, string text)
        {
            return tags.Tag("label", text);
        }

        public static Tag Em(this TagGenerator tags, string text)
        {
            return tags.Tag("em", text);
        }

        public static Tag Strong(this TagGenerator tags, string text)
        {
            return tags.Tag("strong", text);
        }

        public static Tag Span(this TagGenerator tags, string text)
        {
            return tags.Tag("span", text);
        }

        public static Tag Img(this TagGenerator tags, string imageUrl, string alt)
        {
            return tags.Tag("img").Attr("src", imageUrl).Attr("alt", alt).RenderWhen(() => imageUrl != null);
        }
        public static Tag Img(this TagGenerator tags, string imageUrl)
        {
            return tags.Img(imageUrl, "");
        }

        public static Tag H1(this TagGenerator tags, string text)
        {
            return tags.Tag("h1", text);
        }

        public static Tag Hr(this TagGenerator tags)
        {
            return tags.Tag("hr").RenderWhen(delegate { return true; });
        }

        public static Tag Hn(this TagGenerator tags, int headingLevel, string text)
        {
            if (headingLevel <= 0 || headingLevel > 6)
                throw new ArgumentException("Invalid heading level, 1-6 supported");

            return tags.Tag("h" + headingLevel, text);
        }

        public static Tag Span(this TagGenerator tags, object value)
        {
            if (value != null)
                return tags.Span(value.ToString());
            return tags.Span(null);
        }

        public static Tag SpanFormat(this TagGenerator tags, string format, params object[] arguments)
        {
            return tags.Span(format.Format(arguments));
        }
    }
}